package com.baitiaojun.web.core.utils;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 支持servletRequest的工具组件
 */
@Slf4j
public class WebUtils {

    /**
     * 从HttpServletRequest获取json请求参数解析成字符串
     * @param request
     * @return
     */
    public static String getJsonStr(HttpServletRequest request) {
        StringBuilder builder = new StringBuilder();
        try (ServletInputStream inputStream = request.getInputStream();
             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) {
            String content;
            while ((content = bufferedReader.readLine()) != null) {
                builder.append(content);
            }
        } catch (IOException e) {
            log.warn("json参数获取异常");
        }
        return builder.toString();
    }
}